# coding: utf-8
import sys

from PySide6.QtCore import Qt
from PySide6.QtGui import QImage, QPixmap

from qeventmanager import qevent_manager

from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton


class Demo(QWidget):
    def __init__(self):
        super().__init__()
        self.verticalLayout = QVBoxLayout(self)
        self.pushButton = QPushButton(self)
        self.pushButton.setText("Click me")

        self.label = QLabel(self)
        self.label.setText("Hello, world!")
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label.setScaledContents(True)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.pushButton)
        self.verticalLayout.addWidget(self.label, 1)

        self.pushButton.clicked.connect(self.handle_click)

    def handle_click(self):
        url = './img.jpg'
        qevent_manager.addLoadImageFromFile(url, slot=self.handle_image, use_pil=False)

    def handle_image(self, image: QImage):
        image = image.scaledToWidth(self.verticalLayout.geometry().width())
        self.label.setPixmap(QPixmap.fromImage(image))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    demo.resize(1000, 600)
    demo.show()
    app.aboutToQuit.connect(qevent_manager.deleteLater)
    sys.exit(app.exec())
